<?php

namespace App\Models;

use App\Traits\GetByOpenid;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Overtrue\Socialite\AccessToken;
use Overtrue\Socialite\User;

/**
 * App\Models\Member
 *
 * @property int $id
 * @property string $openid 微信公众平台openid
 * @property string $name
 * @property string $nickname
 * @property string $email
 * @property string $avatar
 * @property int $sex
 * @property string $language
 * @property string $city
 * @property string $province
 * @property string $country
 * @property string $access_token
 * @property string $refresh_token
 * @property string|null $expires_time
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @property string|null $deleted_at
 * @property-read \App\Models\MemberAccount $member_account
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\WechatUpload[] $wechat_uploads
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Member onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereAccessToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereCountry($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereExpiresTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereLanguage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereNickname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereOpenid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereRefreshToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Member withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Member withoutTrashed()
 * @mixin \Eloquent
 */
class Member extends Model
{
    use GetByOpenid, SoftDeletes;

    const SEXES = [
        1 => '♂',
        2 => '♀',
    ];

    public function wechat_uploads()
    {
        return $this->hasMany(WechatUpload::class, 'openid', 'openid');
    }

    public function member_account()
    {
        return $this->hasOne(MemberAccount::class, 'openid', 'openid');
    }

    protected $fillable = ['openid'];

    public function easyWechatFill(User $user)
    {
        $accessTokenObj = $user->getToken();
        $original = $user->getOriginal();
        $this->openid = $user->getId();
        $this->name = $user->getName();
        $this->nickname = $user->getNickname();
        $this->avatar = to_https_url($user->getAvatar());
        $this->sex = isset($original['sex']) ? $original['sex'] : 0;
        $this->language = isset($original['language']) ? $original['language'] : '';
        $this->city = isset($original['city']) ? $original['city'] : '';
        $this->province = isset($original['province']) ? $original['province'] : '';
        $this->country = isset($original['country']) ? $original['country'] : '';
        $this->access_token = $accessTokenObj instanceof AccessToken ? $accessTokenObj->access_token : '';
        $this->refresh_token = $accessTokenObj instanceof AccessToken ? $accessTokenObj->refresh_token : '';
        $expires_in = $accessTokenObj instanceof AccessToken ? $accessTokenObj->expires_in : 0;
        if ($expires_in > 0) {
            $this->expires_time = Carbon::now()->addSeconds($expires_in)->toDateTimeString();
        } else {
            $this->expires_time = null;
        }
    }

    public function member_withdraw_logs()
    {
        $this->hasMany(MemberWithdrawLogs::class, 'openid', 'openid');
    }

    public function getSexAttribute()
    {
        if (!isset($this->attributes['sex'])) {
            return '◯';
        }
        if (array_key_exists($this->attributes['sex'], self::SEXES)) {
            return self::SEXES[$this->attributes['sex']];
        }
        return '◯';
    }
}
